const moment = require('../plug/moment.min.js');

//方式1格式化日期
function dateFormat(dataStr, pattern = 'YYYY-MM-DD') {
  return moment(dataStr).format(pattern)
}

//方式2：格式化日期
function formatTime(dataStr) {
  var nowdate = ""
  //console.log(nowdate)
  var _tempstr = dataStr;
  var str = "";
  nowdate = moment(new Date()).format("YYYY-MM-DD HH:mm:ss"); //当前时间
  str = moment(dataStr).format("YYYY-MM-DD HH:mm:ss"); //前台调用传的时间
  var nowdate2
  try {
    var date1 = new Date(str)
    var date2 = new Date(nowdate)
    var s1 = date1.getTime(),
      s2 = date2.getTime();
    var total = (s2 - s1) / 1000;
    var day = parseInt(total / (24 * 60 * 60)); //计算整数天数
    var afterDay = total - day * 24 * 60 * 60; //取得算出天数后剩余的秒数
    var hour = parseInt(afterDay / (60 * 60)); //计算整数小时数
    var afterHour = total - day * 24 * 60 * 60 - hour * 60 * 60; //取得算出小时数后剩余的秒数
    var min = parseInt(afterHour / 60); //计算整数分
    var afterMin = total - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60; //取得算出分后剩余的秒数
    //console.log(hour)
    var minRes = (hour * 60 + min);
    //console.log(minRes)
    if (moment(new Date()).format("YYYY-MM-DD") === moment(dataStr).format("YYYY-MM-DD")) {
      if (min <= 1) {
        return "刚刚";
      } else if (minRes > 1 && minRes <= 60) {
        return minRes + "分钟前";
      } else if (minRes > 60 && minRes <= 120) {
        return "1小时前";
      } else if (minRes > 120 && minRes <= 180) {
        return "2小时前";
      } else if (minRes > 180) {
        var strRes = "";
        strRes = moment(dataStr).format("YYYY-MM-DD HH:mm:ss");
        console.log(strRes)
        var myDate = new Date(strRes); //实例一个时间对象；
        var newHour = "";
        var newMin = "";
        if (myDate.getHours() < 10) {
          newHour = "0" + myDate.getHours();
        } else {
          newHour = myDate.getHours();
        }
        if (myDate.getMinutes() < 10) {
          newMin = "0" + myDate.getMinutes();
        } else {
          newMin = myDate.getMinutes();
        }
        return newHour + ":" + newMin;
      } else {
        return moment(_tempstr).format("YYYY-MM-DD");
      }
    } else {
      if (moment(new Date()).format("YYYY") === moment(dataStr).format("YYYY")) {
        var s3 = moment(dataStr).format("YYYY-MM-DD")
        var arr = new Array();
        arr = s3.split("-");
        return arr[1] + "-" + arr[2];
      } else {
        return moment(_tempstr).format("YYYY-MM-DD");
      }
    }
  } catch (e) {
    return "";
  }
}

//方式3格式化日期
function dateFormat2(dataStr, pattern = 'YYYY-MM-DD HH:mm') {
  return moment(dataStr).format(pattern)
}

//方式4格式化日期
function dateFormat4(dataStr, pattern = 'YYYY-MM-DD HH:mm') {
  if (dataStr == null || dataStr == "" || dataStr == undefined || dataStr == []) {
    return ''
  } else {
    return moment(dataStr).format(pattern)
  }
}

//方式5格式化日期
function dateFormat3(dataStr, pattern = 'YYYY/MM/DD') {
  return moment(dataStr).format(pattern)
}

//方式6格式化日期
function dateFormat6(dataStr, pattern = 'YYYY/MM/DD HH:mm') {
  return moment(dataStr).format(pattern)
}

//时间戳转日期
function formatDatetwo(time) {
  var format = 'yyyy-MM-dd hh:mm';
  if (time == '') { } else {
    var re = /-?\d+/;
    var m = re.exec(time);
    var d = new Date(parseInt(m[0]));
    var o = {
      'M+': d.getMonth() + 1, //month
      'd+': d.getDate(), //day
      'h+': d.getHours(), //hour
      'm+': d.getMinutes(), //minute
      's+': d.getSeconds(), //second
      'q+': Math.floor((d.getMonth() + 3) / 3), //quarter
      S: d.getMilliseconds() //millisecond
    };

    if (/(y+)/.test(format)) {
      format = format.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
      if (new RegExp('(' + k + ')').test(format)) {
        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
      }
    }
  }
  return format;
}

//报警类型过滤
function formatTypeData(typess) {
  var formatType = '';
  if (typess == 'security_monitoring') {
    formatType = '安防报警';
  } else if (typess == 'parking') {
    formatType = '停车场报警';
  } else if (typess == 'device') {
    formatType = '设备报警';
  } else if (typess == 'face_access') {
    formatType = '人脸门禁报警';
  }
  return formatType;
}

//报警子类型过滤
function formatType(typesss) {
  var formatType = '';
  if (typesss == 'invade') {
    formatType = '入侵检测';
  } else if (typesss == 'retention') {
    formatType = '非法滞留';
  } else if (typesss == 'fire') {
    formatType = '火灾监控';
  } else if (typesss == 'crowd') {
    formatType = '异常聚集';
  } else if (typesss == 'tumble') {
    formatType = '摔倒';
  } else if (typesss == 'inout') {
    formatType = '进出';
  } else if (typesss == 'offline') {
    formatType = '离线';
  } else if (typesss == 'fault') {
    formatType = '报错';
  } else if (typesss == 'elevator') {
    formatType = '滞留';
  } else if (typesss == 'delayed') {
    formatType = '延误';
  } else if (typesss == 'online') {
    formatType = '在线';
  }
  return formatType;
}

//报警级别过滤
function formatTypeLevel(levelss) {
  var formatType = '';
  if (levelss == '1') {
    formatType = '通知类警告';
  } else if (levelss == '2') {
    formatType = '轻警告';
  } else if (levelss == '3') {
    formatType = '中级警告';
  } else if (levelss == '4') {
    formatType = '严重警告';
  } else if (levelss == '5') {
    formatType = '致命警告';
  }
  return formatType;
}

//工单进度信息状态
function flownameformat(str, state) {
  var flowname = ""
  if (str.indexOf('创建任务') != -1) {
    flowname = '创建'
  } else if (str.indexOf('转交任务') != -1) {
    flowname = '已转派'
  } else if (str.indexOf('分配任务') != -1) {
    flowname = '已分配'
  } else if (str.indexOf('接受工单') != -1) {
    flowname = '已接受'
  } else if (str.indexOf('维修记录') != -1) {
    if (state == '10' || state == '4') {
      flowname = '已修好'
    } else if (state == '11') {
      flowname = '审核中'
    } else {
      flowname = '维修中'
    }
  } else if (str.indexOf('工单返修') != -1) {
    if (state == '10' || state == '4') {
      flowname = '已返修'
    } else if (state == '11') {
      flowname = '审核中'
    } else {
      flowname = '返修中'
    }
  } else if (str.indexOf('完成任务') != -1) {
    flowname = '已完成'
  } else if (str.indexOf('评价') != -1) {
    flowname = '已评价'
  } else if (str.indexOf('完成任务') != -1) {
    flowname = '已完成'
  } else if (str.indexOf('已关闭') != -1) {
    flowname = '已关闭'
  } else if (str.indexOf('撤销工单') != -1) {
    flowname = '已撤销'
  } else if (str.indexOf('催单') != -1) {
    flowname = '已催单'
  } else if (str.indexOf('审批通过') != -1) {
    flowname = '已审批'
  } else {

  }
  return flowname;
}

//获取当前日期
function getNowDate() {
  return moment(new Date()).format("YYYY-MM-DD HH:mm")
}

//获取当前时间
function getNowTime() {
  return moment(new Date()).format("YYYY-MM-DD HH:mm:ss")
}


module.exports = {
  formatTime: formatTime,
  dateFormat: dateFormat,
  dateFormat2: dateFormat2,
  flownameformat: flownameformat,
  getNowDate: getNowDate,
  formatDatetwo: formatDatetwo,
  formatTypeData: formatTypeData,
  formatTypeLevel: formatTypeLevel,
  formatType: formatType,
  getNowTime: getNowTime,
  dateFormat3: dateFormat3,
  dateFormat6: dateFormat6,
  dateFormat4: dateFormat4,
}